package com.hf.wujian.model.imp;

import com.hf.wujian.constant.ModelConstant;
import com.hf.wujian.exception.CreateRoomException;
import com.hf.wujian.model.GameHall;
import com.hf.wujian.model.GameRoom;
import com.hf.wujian.model.GameUser;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 游戏房间实现类
 *
 * @author hf
 * @date 2018/11/23
 * @version 1.0
 *
 */
public class BSHGameRoom implements GameRoom{

    /**
     * 房间名
     */
    private String roomName;
    /**
     * 房主（创建者、管理员）
     */
    private GameUser owner;
    /**
     * 房间内的用户
     */
    private List<GameUser> gameUsers;
    /**
     * 房间状态
     * ps:房间状态初始化为准备阶段
     */
    private int roomStatus = ModelConstant.ROOM_PREPARE;

    /**
     * 无间风云游戏规则类
     */
    private BSHAGame bshaGame;

    /**
     * 房间读写锁
     */
    private ReadWriteLock roomRwlock = new ReentrantReadWriteLock(true);

    /**
     *
     * @param roomName 房间名
     * @param owner 创建用户
     */
    public BSHGameRoom(String roomName,GameUser owner) throws Exception {
        this.roomName = roomName;
        this.owner = owner;
        gameUsers = new ArrayList<>();
        gameUsers.add(owner);
        owner.createRoom(this);
    }


    /**
     * @param gameUser 游戏用户
     */
    @Override
    public void addGameUser(GameUser gameUser) {
        roomRwlock.writeLock().lock();
        try {
            if(gameUsers == null){
                return;
            }
            gameUsers.add(gameUser);
        }finally {
            roomRwlock.writeLock().unlock();
        }
    }

    /**
     * @param gameUser 游戏用户
     */
    @Override
    public void removeGameUser(GameUser gameUser) {
        roomRwlock.writeLock().lock();
        try {
            if(gameUsers == null){
                return;
            }

            if (gameUsers.size()==1) {
                //destroyRoom();
            }
            gameUsers.remove(gameUser);
        }finally {
            roomRwlock.writeLock().unlock();
        }
    }

    /**
     *
     * 销毁房间
     */
    @Override
    public void destroyRoom(GameUser gameUser) {
        roomRwlock.writeLock().lock();
        try{
            if(gameUsers == null){
                return;
            }
            //如果用户不是房主（管理员）,销毁房间失败
            if(gameUser != owner){
                return;
            }
            //从房间成员中移除房主
            gameUsers.remove(gameUser);
            //向每个成员发送房间解散的消息
            for (GameUser gameUser1 : gameUsers) {
                gameUser1.cancelRoom(ModelConstant.USER_REMOVED_ROOM);
            }
            gameUsers.clear();
            gameUsers = null;
        }finally {
            roomRwlock.writeLock().unlock();
        }
    }

    /**
     * 游戏初始化接口
     */
    @Override
    public void gameInit(GameUser gameUser) {
        roomRwlock.writeLock().lock();
        try {

            gameUsers.remove(gameUser);
        }finally {
            roomRwlock.writeLock().unlock();
        }
    }

    /**
     * 获得所有用户用户名接口
     * @return 用户名集合
     */
    @Override
    public List<String> getAllUserName() {
        List<String> usersName = new ArrayList<>();
        roomRwlock.readLock().lock();
        try {
            for (GameUser gameUser : gameUsers) {
                usersName.add(gameUser.getNickName());
            }
        }finally {
            roomRwlock.readLock().unlock();
        }
        return usersName;
    }

    @Override
    public String getRoomName() {
        return this.roomName;
    }

    /**
     * 获得房间名接口
     * @param msg 消息
     * @param uuid 消息发送用户的uuid
     */
    @Override
    public void broadcastToAllUser(String msg,String uuid) {
        roomRwlock.readLock().lock();
        try {
            for (GameUser gameUser : gameUsers) {
                if(!gameUser.getUuid().equals(uuid)){
                    gameUser.sendMessageToUser(msg);
                }
            }
        }finally {
            roomRwlock.readLock().unlock();
        }
    }

    /**
     * 获得房主
     * @return 房主对象
     */
    @Override
    public GameUser getHost() {
        GameUser gameUser;
        roomRwlock.readLock().lock();
        try {
            gameUser = this.owner;
        }finally {
            roomRwlock.readLock().unlock();
        }
        return gameUser;
    }
}
